﻿namespace NTLib.Pipe.Windows.Descriptors
{
    using Test.Resources;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using UTest;
    using Descriptors;
    using Streaming.Formats;
    using Core.Descriptors;

    [TestClass]
    public class NamedPipeTargetDescriptorUTest : BaseTestClass
    {
        #region  Methods


        /// <summary>
        /// Test if the initialized and default fields are well sets
        /// </summary>
        [TestMethod]
        [TestCategory(TestHelper.CATEGORIE_DESCRIPTORS)]
        public void NamedPipeTargetDescriptor_DefaultFields()
        {
            Guid id = Guid.NewGuid();
            var pipeNamed = Convert.ToString(Guid.NewGuid());

            var idFormater = Guid.NewGuid();
            var formaterName = nameof(SimpleJsonPackageFormater);

            var formater = new Descriptor(idFormater, formaterName);

            Assert.AreEqual(idFormater, formater.Id);
            Assert.AreEqual(formaterName, formater.Name);

            var descriptor = new NamedPipeTargetDescriptor(id, formater, true, pipeNamed);

            Assert.AreEqual(id, descriptor.Id);
            Assert.IsNull(descriptor.Name);
            Assert.AreEqual(pipeNamed, descriptor.PipeName);
            Assert.AreEqual(formater, descriptor.PackageFormater);
            Assert.IsTrue(descriptor.IsServerHost);
            Assert.IsNull(descriptor.ConnectionTimeOut);
        }

        /// <summary>
        /// Test if the initialized fields are well sets
        /// </summary>
        [TestMethod]
        [TestCategory(TestHelper.CATEGORIE_DESCRIPTORS)]
        public void NamedPipeTargetDescriptor_InitializedFields()
        {
            Guid id = Guid.NewGuid();
            var pipeNamed = Convert.ToString(Guid.NewGuid());
            var named = Convert.ToString(Guid.NewGuid());

            var idFormater = Guid.NewGuid();
            var formaterName = nameof(SimpleJsonPackageFormater);

            var formater = new Descriptor(idFormater, formaterName);

            Assert.AreEqual(idFormater, formater.Id);
            Assert.AreEqual(formaterName, formater.Name);

            TimeSpan timeout = TimeSpan.FromSeconds(42);

            var descriptor = new NamedPipeTargetDescriptor(id, formater, true, pipeNamed, timeout, named);

            Assert.AreEqual(id, descriptor.Id);
            Assert.AreEqual(named, descriptor.Name);
            Assert.AreEqual(pipeNamed, descriptor.PipeName);
            Assert.AreEqual(formater, descriptor.PackageFormater);
            Assert.IsTrue(descriptor.IsServerHost);
            Assert.AreEqual(timeout, descriptor.ConnectionTimeOut);
        }

        /// <summary>
        /// Test if the clone process work correctly
        /// </summary>
        [TestMethod]
        [TestCategory(TestHelper.CATEGORIE_DESCRIPTORS)]
        public void NamedPipeTargetDescriptor_Clone()
        {
            Guid id = Guid.NewGuid();
            var pipeNamed = Convert.ToString(Guid.NewGuid());
            var named = Convert.ToString(Guid.NewGuid());

            var idFormater = Guid.NewGuid();
            var formaterName = nameof(SimpleJsonPackageFormater);

            var formater = new Descriptor(idFormater, formaterName);

            Assert.AreEqual(idFormater, formater.Id);
            Assert.AreEqual(formaterName, formater.Name);

            TimeSpan timeout = TimeSpan.FromSeconds(42);

            var descriptor = new NamedPipeTargetDescriptor(id, formater, true, pipeNamed, timeout, named);

            Assert.AreEqual(id, descriptor.Id);
            Assert.AreEqual(named, descriptor.Name);
            Assert.AreEqual(pipeNamed, descriptor.PipeName);
            Assert.AreEqual(formater, descriptor.PackageFormater);
            Assert.IsTrue(descriptor.IsServerHost);
            Assert.AreEqual(timeout, descriptor.ConnectionTimeOut);

            var clone = descriptor.Clone() as NamedPipeTargetDescriptor;

            Assert.IsNotNull(clone);
            Assert.AreNotEqual(clone, descriptor);

            Assert.AreEqual(id, clone.Id);
            Assert.AreEqual(named, clone.Name);
            Assert.AreEqual(pipeNamed, clone.PipeName);
            Assert.AreEqual(formater, clone.PackageFormater);
            Assert.IsTrue(clone.IsServerHost);
            Assert.AreEqual(timeout, clone.ConnectionTimeOut);
        }

        /// <summary>
        /// Test if the equals function check every field correctly
        /// </summary>
        [TestMethod]
        [TestCategory(TestHelper.CATEGORIE_DESCRIPTORS)]
        public void NamedPipeTargetDescriptor_Equals()
        {
            Guid id = Guid.NewGuid();
            var pipeNamed = Convert.ToString(Guid.NewGuid());
            var named = Convert.ToString(Guid.NewGuid());

            var idFormater = Guid.NewGuid();
            var formaterName = nameof(SimpleJsonPackageFormater);

            var formater = new Descriptor(idFormater, formaterName);

            Assert.AreEqual(idFormater, formater.Id);
            Assert.AreEqual(formaterName, formater.Name);

            TimeSpan timeout = TimeSpan.FromSeconds(42);

            var descriptor = new NamedPipeTargetDescriptor(id, formater, true, pipeNamed, timeout, named);

            Assert.AreEqual(id, descriptor.Id);
            Assert.AreEqual(named, descriptor.Name);
            Assert.AreEqual(pipeNamed, descriptor.PipeName);
            Assert.AreEqual(formater, descriptor.PackageFormater);
            Assert.IsTrue(descriptor.IsServerHost);
            Assert.AreEqual(timeout, descriptor.ConnectionTimeOut);

            var clone = descriptor.Clone() as NamedPipeTargetDescriptor;

            Assert.IsNotNull(clone);
            Assert.AreNotEqual(clone, descriptor);

            Assert.AreEqual(id, clone.Id);
            Assert.AreEqual(named, clone.Name);
            Assert.AreEqual(pipeNamed, clone.PipeName);
            Assert.AreEqual(formater, clone.PackageFormater);
            Assert.IsTrue(clone.IsServerHost);
            Assert.AreEqual(timeout, clone.ConnectionTimeOut);

            Assert.IsTrue(clone.Equals(clone));
            Assert.IsTrue(descriptor.Equals(descriptor));

            Assert.IsTrue(clone.Equals(descriptor));
            Assert.IsTrue(descriptor.Equals(clone));

            clone = new NamedPipeTargetDescriptor(Guid.NewGuid(), formater, true, pipeNamed, timeout, named);

            Assert.IsFalse(clone.Equals(descriptor));
            Assert.IsFalse(descriptor.Equals(clone));

            clone = new NamedPipeTargetDescriptor(id, null, true, pipeNamed, timeout, named);

            Assert.IsFalse(clone.Equals(descriptor));
            Assert.IsFalse(descriptor.Equals(clone));

            clone = new NamedPipeTargetDescriptor(id, formater, false, pipeNamed, timeout, named);

            Assert.IsFalse(clone.Equals(descriptor));
            Assert.IsFalse(descriptor.Equals(clone));

            clone = new NamedPipeTargetDescriptor(id, formater, true, nameof(pipeNamed), timeout, named);

            Assert.IsFalse(clone.Equals(descriptor));
            Assert.IsFalse(descriptor.Equals(clone));

            clone = new NamedPipeTargetDescriptor(id, formater, true, pipeNamed, null, named);

            Assert.IsFalse(clone.Equals(descriptor));
            Assert.IsFalse(descriptor.Equals(clone));

            clone = new NamedPipeTargetDescriptor(id, formater, true, pipeNamed, timeout, nameof(named));

            Assert.IsFalse(clone.Equals(descriptor));
            Assert.IsFalse(descriptor.Equals(clone));

            clone = new NamedPipeTargetDescriptor(id, formater, true, pipeNamed, timeout, named);

            Assert.IsTrue(clone.Equals(descriptor));
            Assert.IsTrue(descriptor.Equals(clone));
        }

        #endregion
    }
}
